using System;
using System.Collections.Generic;
using System.Text;

using Danser_Control.Managers;
using Danser_Entity.Entities;
using Danser_Entity.Queries.Filters;
using Danser_Entity.Utils;



namespace Danser_AJAXEnabledWebApplication.DataSourceManagers
{
    /// <summary>
    /// Contains Data Source support methods for the validation template entities
    /// </summary>
    public class ValidationsDataSourceManager
    {
        #region Members:

        /// <summary>
        /// Local list. holds a copy of the table's records in DB,
        /// + may be 1 more empty record representing a new row in datagrid
        /// </summary>
        private IList<ValidationTemplate> _templates;

        /// <summary>
        /// Control layer interface to access the DB
        /// </summary>
        IValidationManager manager;

        #endregion

        #region C'tor:
        
        /// <summary>
        /// creates a ValidationsDataSourceManager instance
        /// </summary>
        public ValidationsDataSourceManager()
        {
            //Get validations from proper control interface
            manager = ManagerFactory.ValidationManager();
            _templates = manager.GetValidationTemplates((IList<int>)null);
        }

        #endregion

        #region Query Methods:

        /// <summary>
        /// CREATE method implementation
        /// </summary>
        /// <param name="ID">generated id</param>
        /// <param name="name">template name</param>
        /// <param name="appliedOnDefault">set to true if validation executed by default when loading new data</param>
        /// <param name="mailList">mailing list</param>
        /// <param name="expression">the validation expression</param>
        public void AddValidation(
            int ID,
            string name, 
            bool appliedOnDefault, 
            String mailList, 
            string expression)
        {
            //Do nothing. 
            //This is a "virtual" call that will cause data binding of the datagrid.
            //When data binding occurs, it'll be notified that there's a new row
            //to display. see GetValidation() below.
        }

        /// <summary>
        /// SELECT method implementation
        /// </summary>
        /// <param name="newRow">set to true if new row exists in UI</param>
        /// <param name="filter">filter used in WHERE clause</param>
        /// <returns>list of validation templates</returns>
        public IList<ValidationTemplate> GetValidation(bool newRow, String filter)
        {
            //show the new row when grid performs data binding
            if (newRow) //add new row to the copy local list
            {
                //insert only to the local list. DB doesn't know this row
                _templates.Insert(0, new ValidationTemplate());
            }
            else //get users according to filter
            {
                _templates = manager.GetValidationTemplates(filter);
            }
            return _templates;
        }

        /// <summary>
        /// UPDATE method implementation
        /// </summary>
        /// <param name="ID">generated id</param>
        /// <param name="name">template name</param>
        /// <param name="appliedOnDefault">set to true if validation executed by default when loading new data</param>
        /// <param name="mailList">mailing list</param>
        /// <param name="expression">the validation expression</param>
        /// <param name="newRow">set to true if new row exists in UI</param>
        public void MoidifyValidation( 
            int ID,
            String name, 
            bool appliedOnDefault, 
            String mailList, 
            String expression,
            bool newRow)
        {       
            //if activated for a new row
            if (newRow)
            {
                //call the interface's add method
                manager.CreateTemplate(name, appliedOnDefault, mailList, expression);
            }
            else
            {
                //send request to DB
                manager.ModifyTemplateDetails(ID, mailList, appliedOnDefault);
            }
        }

        /// <summary>
        /// DELETE method implementation
        /// </summary>
        /// <param name="ID">the template id to remove</param>
        /// <param name="newRow">set to true if new row exists in UI</param>
        public void RemoveValidation(int ID, bool newRow)
        {
            if (!newRow) 
            {
                //send request to DB
                manager.RemoveValidationTemplate(ID);
            }

        }

        #endregion
    }
}
